<template>
  <div>
    <label>
      用户名: <input type="text" v-model="loginInfo.username"/>  <br>
    </label>
    <label>
      密码: <input type="text" v-model="loginInfo.password"/>  <br>
    </label>
    <button type="button" @click="login">登录</button>
    <br/>
    <router-link to="/register">新用户注册</router-link>
  </div>
</template>

<script>
  export default {
    name: 'SpringSecurityLogin',
    data() {
      return {
        loginInfo: {
          username: "",
          password: ""
        }
      }
    },
    methods: {
      login() {
        let url = "login";
        if (this.validate()) {
          this.postRequest(url, this.loginInfo).then(res => {
            console.log(res);
            if (res.code === 0) {
              // 存储token
              const tokenStr = res.data['tokenHead'] + res.data.token;
              console.log("获取到token >>>>>>>>>>>>>>>", tokenStr);
              window.sessionStorage.setItem('tokenStr', tokenStr);
              this.$router.replace("/afterlogin");
            }
          }).catch();
        }

        // if (this.loginInfo.username === 'admin' && this.loginInfo.password === '123') {
        //   alert("登录成功");
        //   let path = this.$route.query.redirect;
        //   console.log(path);
        //   this.$router.replace("/afterlogin");
        // }
      },
      validate() {
        return this.loginInfo.username !== "" && this.loginInfo.password !== "";
      }
    }
  }
</script>

<style scoped>

</style>
